﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Bowerbird.Core.Storage;

namespace Bowerbird.Core.Storage
{
    public static class ContactDataRepository
    {
        internal static ContactData AddNew(string userId, int contactDataType, string name, string value, string visibleTo)
        {
            ContactData contactData = new ContactData 
            {
                UserId = userId,
                ContactDataType = contactDataType,
                Name = name,
                Value = value,
                VisibleTo = visibleTo
            };

            contactData = RepositoryBase<ContactData>.SaveOrUpdate(contactData);

            return contactData;
        }

        internal static IEnumerable<ContactData> GetContactData(string userId)
        {
            IList exp = new ArrayList();

            exp.Add(NHibernate.Criterion.Expression.Eq("UserId", userId));

            IList<ContactData> list = NHibernateManager.GetObjectList<ContactData>(exp);

            return list;
        }

        internal static void Remove(string userId, string id)
        {
            IList exp = new ArrayList();

            exp.Add(NHibernate.Criterion.Expression.And(
                        NHibernate.Criterion.Expression.Eq("UserId", userId),
                        NHibernate.Criterion.Expression.Eq("Id", id)));

            IList<ContactData> list = NHibernateManager.GetObjectList<ContactData>(exp);
            
            var contactData = list.FirstOrDefault();

            if (contactData != null)
                RepositoryBase<ContactData>.Delete(contactData);
        }
    }
}
